package com.gx.citypicker;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.gx.city_picker.CityPickerDialog;
import com.gx.city_picker.entity.AreaEntity;
import com.gx.city_picker.entity.CityEntity;
import com.gx.city_picker.entity.ProvinceEntity;
import com.gx.city_picker.entity.TownEntity;

public class MainActivity extends AppCompatActivity implements CityPickerDialog.AddressListener {

    private TextView result_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.level_1).setOnClickListener(v -> open(CityPickerDialog.CITY_PICKER_LEVEL1));

        findViewById(R.id.level_2).setOnClickListener(v -> open(CityPickerDialog.CITY_PICKER_LEVEL2));

        findViewById(R.id.level_3).setOnClickListener(v -> open(CityPickerDialog.CITY_PICKER_LEVEL3));

        findViewById(R.id.level_4).setOnClickListener(v -> open(CityPickerDialog.CITY_PICKER_LEVEL4));

        result_tv = findViewById(R.id.result_tv);
    }

    private void open(int level){
        CityPickerDialog.Build(this)
                        .setLevel(level) // 设置联动等级
                        .setAddressListener(this) // 添加选择回调监听
                        .setTitle("请选择地址") // 设置标题，如需要隐藏显示设置为 null 即可
                        //.setTitleColor(R.color.text_999) // 设置标题的颜色
                        //.setSelectColor(R.color.primary) // 设置选中的颜色
                        //.setDefaultColor(R.color.text_color) // 设置未选中的颜色
                        //.setLineColor(R.color.primary) // 设置选中的线的颜色
                        .show();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void callBack(int level, ProvinceEntity province, CityEntity city, AreaEntity area, TownEntity town) {
        if(level == CityPickerDialog.CITY_PICKER_LEVEL1){
            result_tv.setText("您选择的地址：" + province.provinceName);
        }
        else if(level == CityPickerDialog.CITY_PICKER_LEVEL2){
            result_tv.setText("您选择的地址：" + province.provinceName + city.cityName);
        }
        else if(level == CityPickerDialog.CITY_PICKER_LEVEL3){
            result_tv.setText("您选择的地址：" + province.provinceName + city.cityName + area.areaName);
        }
        else if(level == CityPickerDialog.CITY_PICKER_LEVEL4){
            result_tv.setText("您选择的地址：" + province.provinceName + city.cityName + area.areaName + town.townName);
        }
    }
}